package state;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: bjc
 * Date: 2025-08-17
 * Time: 14:43
 */
public class ThreadDemo1 {
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(() -> {
            System.out.println("线程开始执行，状态：" + Thread.currentThread().getState());
            for (int i = 0; i < 5; i++) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        // 线程创建后，状态为NEW
        System.out.println("创建线程后，状态：" + t.getState()); // NEW

        // 启动线程后，状态变为RUNNABLE
        t.start();
        System.out.println("启动线程后，状态：" + t.getState()); // RUNNABLE

        // 等待一段时间，观察线程状态变化
        Thread.sleep(100);
        System.out.println("运行中，状态：" + t.getState()); // RUNNABLE 或 TIMED_WAITING

        // 等待线程执行完毕，状态变为TERMINATED
        t.join();
        System.out.println("线程结束后，状态：" + t.getState()); // TERMINATED
    }
}

